/*
 *  Copyright 2008-2010 www.jrails.org
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.rails.extjs.form;

import org.rails.extjs.Option;
import org.rails.extjs.RegExp;
import org.rails.extjs.javascript.Function;

public class TextField extends Field {
	
	public final static String VTYPE_ALPHA = "alpha"; 
	public final static String VTYPE_ALPHANUM = "alphanum"; 
	public final static String VTYPE_EMAIL = "email"; 
	public final static String VTYPE_URL = "url"; 

	public TextField() {
		super();
	}

	public TextField(Option configOptions) {
		super(configOptions);
	}

	public TextField(String name) {
		super(name);
	}
	
	public TextField(String name,String fieldLabel) {
		super(name,fieldLabel);
	}
	
	public TextField(String name,String fieldLabel,Object value) {
		super(name,fieldLabel,value);
	}
	
	public void setAllowBlank(Boolean allowBlank){
		addOption("allowBlank", allowBlank);
	}
	
	public Boolean getAllowBlank(){
		return getBoolean("allowBlank",true);
	}
	
	public void setBlankText(String blankText){
		addOption("blankText", blankText);
	}
	
	public String getBlankText(){
		return getString("blankText", "This field is required");
	}
	
	public void setDisableKeyFilter(Boolean disableKeyFilter){
		addOption("disableKeyFilter", disableKeyFilter);
	}
	
	public Boolean isDisableKeyFilter(){
		return getBoolean("disableKeyFilter", false);
	}
	
	public void setEmptyClass(String emptyClass){
		addOption("emptyClass",emptyClass);
	}
	
	public String getEmptyClass(){
		return getString("emptyClass","x-form-empty-field");
	}
	
	public void setEmptyText(String emptyText){
		addOption("emptyText", emptyText);
	}
	
	public String getEmptyText(){
		return getString("emptyText", "");
	}
	
	public void setEnableKeyEvents(Boolean enableKeyEvents){
		addOption("enableKeyEvents", enableKeyEvents);
	}
	
	public Boolean isEnableKeyEvents(){
		return getBoolean("enableKeyEvents", false);
	}
	
	public void setGrow(Boolean grow){
		addOption("grow", grow);
	}
	
	public Boolean isGrow(){
		return getBoolean("grow", false);
	}
	
	public void setGrowMax(Integer growMax){
		addOption("growMax", growMax);
	}
	
	public Integer getGrowMax(){
		return getInteger("growMax", 800);
	}
	
	public void setGrowMin(Integer growMin){
		addOption("growMin", growMin);
	}
	
	
	public Integer getGrowMin(){
		return getInteger("growMin", 30);
	}
	
	public void setMaskRegexp(String maskRe){
		addOption("maskRe",new RegExp(maskRe));
	}
	
	public RegExp getMaskRegexp(){
		return (RegExp)getOption("maskRe");
	}
	
	public void setMaxLength(Integer maxLength){
		addOption("maxLength", maxLength);
	}
	
	public Integer getMaxLength(){
		return getInteger("maxLength", Integer.MAX_VALUE).intValue();
	}
	
	public void setMaxLengthText(String maxLengthText){
		addOption("maxLengthText", maxLengthText);
	}
	
	public String getMaxLengthText(){
		return getString("maxLengthText", "The maximum length for this field is {maxLength}");
	}
	
	public void setMinLength(Integer minLength){
		addOption("minLength", minLength);
	}
	
	public Integer getMinLength(){
		return getInteger("minLength", 0);
	}
	
	public void setMinLengthText(String minLengthText){
		addOption("minLengthText", minLengthText);
	}
	
	public String getMinLengthText(){
		return getString("minLengthText","The minimum length for this field is {minLength}");
	}
	
	public void setRegex(String regex){
		addOption("regex", new RegExp(regex));
	}
	
	public RegExp getRegex(){
		return (RegExp)getOption("regex");
	}
	
	public void setRegexText(String regexText){
		addOption("regexText", regexText);
	}
	
	public String getRegexText(){
		return getString("regexText", "");
	}
	
	public void setSelectOnFocus(Boolean selectOnFocus){
		addOption("selectOnFocus", selectOnFocus);
	}
	
	public Boolean isSelectOnFocus(){
		return getBoolean("selectOnFocus", false);
	}
	
	public void setStripCharsRe(String stripCharsRe){
		addOption("stripCharsRe", stripCharsRe);
	}
	
	public String getStripCharsRe(){
		return getString("stripCharsRe", "");
	}
	
	public void setValidator(Function validator){
		addOption("validator", validator);
	}
	
	public Function setValidator(){
		return (Function)getOption("validator");
	}
	
	public void setVType(String vtype){
		addOption("vtype",vtype);
	}
	
	public String getVType(){
		return getString("vtype","");
	}
	
	public void setVTypeText(String vtypeText){
		addOption("vtypeText",vtypeText);
	}
	
	public String getVTypeText(){
		return getString("vtypeText","");
	}
	
	@Override
	public String getScript() {
		script = new StringBuffer("");
		createItems();
		script.append("new Ext.form.TextField(");
		script.append(createOptions());
		script.append(")");
		return script.toString();
	}

}
